package net.telesing.tcmc.back.logic;

import net.telesing.tcmc.back.dao.SectionDao;
import net.telesing.tcmc.back.model.OrgUserModel;
import net.telesing.tcmc.back.model.SectionModel;
import net.telesing.tcmc.back.model.UserModel;
import net.telesing.tcmc.back.modelqp.SectionQp;
import net.telesing.tcmc.common.LogicException;
import net.telesing.tcmc.common.QueryReturn;

import net.telesing.tcmc.core.HttpUtil;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.nutz.dao.Cnd;
import org.nutz.dao.Sqls;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

/**
 * Created by bys on 2015/11/3.
 */
@IocBean
public class SectionLogic
{
    @Inject
    private SectionDao sectionDao;
    private static final int UNKNOWN        = 0;
    private static final int LINK_NOT_FOUND = 1;
    private static final int REPEAT_ACCOUNT = 2;
    private static final int NOT_FOUND      = 3;

    private static final Logger log = LogManager.getLogger(SectionLogic.class);

    private LogicException error(int code)
    {
        switch (code)
        {
            case LINK_NOT_FOUND:
                return new LogicException(code, "该机构下已存在三个码段！");
            case REPEAT_ACCOUNT:
                return new LogicException(code, "码段已存在！");
            case NOT_FOUND:
                return new LogicException(code, "信息不存在");
            default:
                return new LogicException(code, "未知错误");
        }
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public SectionModel find(long id)
    {
        return sectionDao.fetch(id);
    }

    /**
     * 根据条件查询
     *
     * @param qp
     * @return
     */
    public QueryReturn doList(SectionQp qp, UserModel user)
    {
        if (user.getType() == 3)
        {
            OrgUserModel orgUserModel = sectionDao.fetchIdModel(Cnd.where("UID", "=", user.getId()));
            if (null != orgUserModel)
                qp.setOrgId(orgUserModel.getOrgId());
        }
        return sectionDao.queryPage(qp);
    }

    /**
     * 号段管理 新增 逻辑代码
     *
     * @param section
     * @param creator
     * @return SectionModel
     * @throws LogicException
     */
    public SectionModel create(SectionModel section, long creator) throws LogicException
    {
        Cnd aCnd = Cnd.where("NAME", "=", section.getName());
        Cnd sCnd = Cnd.where("OID", "=", section.getOrgId());
        section.setState(2);
        if (null != sectionDao.fetch(aCnd))
            throw error(REPEAT_ACCOUNT);
        if (sectionDao.fetchList(SectionModel.class, sCnd).size() == 3)
            throw error(LINK_NOT_FOUND);
        return sectionDao.insert(section, creator);
    }

    /**
     * 号段管理 编辑 逻辑代码
     *
     * @param section
     * @param updater
     * @return
     * @throws LogicException
     */
    public int modify(SectionModel section, long updater) throws LogicException
    {
        SectionModel sectionModel = sectionDao.fetch(section.getId());
        if (!sectionModel.getName().equals(section.getName()))
        {
            Cnd aCnd = Cnd.where("NAME", "=", section.getName());
            if (null != sectionDao.fetch(aCnd))
                throw error(REPEAT_ACCOUNT);
        }
        if (sectionModel.getOrgId() != section.getOrgId())
        {
            Cnd sCnd = Cnd.where("OID", "=", section.getOrgId());
            if (sectionDao.fetchList(SectionModel.class, sCnd).size() == 3)
                throw error(LINK_NOT_FOUND);
        }
        return sectionDao.update(section, updater);
    }

    /**
     * 号段状态操作
     *
     * @param id
     * @param updater
     * @param state
     * @throws LogicException
     */
    public void modifyState(long id, long updater, int state) throws LogicException
    {
        if (id != 0)
        {
            try
            {
                String sql = "update tsc_section set  status=" + state + ",upt_user =" + updater + " where id=" + id + "";
                sectionDao.touchSql(Sqls.create(sql));
            }
            catch (Exception e)
            {
                throw error(UNKNOWN);
            }
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @throws LogicException
     */
    public void delete(long[] ids) throws LogicException
    {
        if (ids.length != 0)
        {
            Cnd cnd = Cnd.where("ID", "=", ids[0]);
            Cnd aCnd = Cnd.where("SID", "=", ids[0]);
            String sql = "DELETE FROM tsc_section  $condition";
            String sql2 = "DELETE FROM tsc_certificate  $condition";

            try
            {
                sectionDao.touchSql(Sqls.create(sql2).setCondition(aCnd));
                sectionDao.touchSql(Sqls.create(sql).setCondition(cnd));
            }
            catch (Exception e)
            {
                e.printStackTrace();
                throw error(UNKNOWN);
            }
        }
    }

    /**
     * 智能获取jsp页面
     *
     * @param flg
     * @return Added By Zmt
     */
    public String getJsp(int flg)
    {
        String result;
        if (flg == 1)
            result = "pages.back.section.sect_table";
        else
            result = "pages.back.section.section";
        return result;
    }

    /**
     * 判断用户类型
     *
     * @param user
     * @return
     */
    public boolean checkUser(UserModel user)
    {
        return (3 != user.getType());
    }

}
